package com.neiru.divider.view;

import java.util.ArrayList;
import java.util.List;

import android.content.Context;
import android.content.res.Resources;
import android.graphics.Canvas;
import android.graphics.drawable.Drawable;
import android.graphics.drawable.ShapeDrawable;
import android.graphics.drawable.shapes.RectShape;
import android.util.AttributeSet;
import android.view.View;
import android.widget.Gallery;
import android.widget.LinearLayout;
import android.widget.Gallery.LayoutParams;

import com.neiru.divider.R;
import com.neiru.divider.bean.Ball;
import com.neiru.divider.bean.Divider;
import com.neiru.divider.bean.Position;
import com.neiru.divider.bean.Section;
import com.neiru.divider.constants.Axis;
import com.neiru.divider.constants.BallColor;


public class PlayView extends View {
	private List<Section> sections;
	private Divider divider;
	private List<Drawable> ballShapes = new ArrayList<Drawable>();
	private List<ShapeDrawable> lineShapes = new ArrayList<ShapeDrawable>();
	private ShapeDrawable dividerShape;
	private LinearLayout linearLayout;
	private Drawable backgroundDrawable;
	private Context mContext;
	public PlayView(Context context) {
		super(context);
		this.sections = new ArrayList<Section>();
		this.linearLayout = new LinearLayout(context);
		this.backgroundDrawable = context.getResources().getDrawable(com.neiru.divider.R.drawable.background);
		this.mContext = context;
	}
	
	public PlayView(Context context, AttributeSet attrs) {
		super(context, attrs);
		this.sections = new ArrayList<Section>();
		this.linearLayout = new LinearLayout(context);
	}
	public void inicialize(int width, int height) {
		linearLayout.setLayoutParams(new Gallery.LayoutParams(LayoutParams.FILL_PARENT,LayoutParams.FILL_PARENT));
		linearLayout.setBackgroundColor(android.R.color.white);
		this.backgroundDrawable.setBounds(0, 0, width, height);
		this.setBackgroundColor(android.R.color.white);
		Resources res = mContext.getResources();
		for(Section section: sections) {
			for(Ball ball: section.getBalls()) {
//				ShapeDrawable ballShape = new ShapeDrawable(new OvalShape());
				Drawable ballShape = getBallDrawable(ball,res);
//				ballShape.getPaint().setColor(0xff74AC23);
//				ballShape.getPaint().setColor(0xffffffff);
				Position position = ball.getActualPosition();
				ballShape.setBounds(position.getX().intValue() - ball.getRadius(), 
						position.getY().intValue() - ball.getRadius(), 
						position.getX().intValue() + ball.getRadius(), 
						position.getY().intValue() + ball.getRadius());
				ballShapes.add(ballShape);
			}
		}
	}
	private Drawable getBallDrawable(Ball ball,Resources res) {
		Drawable ballShape = null;
		switch(ball.getColor()) {
		case WHITE:
		case BLACK:
		case RED:
			ballShape = res.getDrawable(R.drawable.ball_red);
			break;
		case BLUE:
		default:
			ballShape = res.getDrawable(R.drawable.ball_blue);
				break;
		}
		return ballShape;
	}
	private void uploadShapes(Canvas canvas) {
		int i = 0;
		for(Section section: sections) {
			for(Ball ball: section.getBalls()) {
				Drawable ballShape = ballShapes.get(i);
				i++;
				Position position = ball.getActualPosition();
				ballShape.setBounds(position.getX().intValue() - ball.getRadius(), 
						position.getY().intValue() - ball.getRadius(), 
						position.getX().intValue() + ball.getRadius(), 
						position.getY().intValue() + ball.getRadius());
				ballShape.draw(canvas);
			}
			if(this.divider != null) {
				if(dividerShape == null) {
					this.dividerShape = new ShapeDrawable(new RectShape());
//					dividerShape.getPaint().setColor(0xff74AC23);
					dividerShape.getPaint().setColor(0xffffffff);
				}
				if(Axis.AXIS_X.equals(divider.getAxis())) {
					Integer y = divider.getStartPosition().getY().intValue();
					Integer startX = divider.getStartPosition().getX().intValue();
					Integer endX = divider.getFinalPosition().getX().intValue();
					this.dividerShape.setBounds(startX, y-2, endX, y+2);
				} else if(Axis.AXIS_Y.equals(divider.getAxis())) {
					Integer x = divider.getStartPosition().getX().intValue();
					Integer startY = divider.getStartPosition().getY().intValue();
					Integer endY = divider.getFinalPosition().getY().intValue();
					this.dividerShape.setBounds(x-2, startY, x+2, endY);
				}
				dividerShape.draw(canvas);
			}
		}
		
		
	}
	
	public void addSection(Section section) {
		this.sections.add(section);
	}
	public List<Section> getSections() {
		return this.sections;
	}
	@Override
	protected void onDraw(Canvas canvas) {
		super.onDraw(canvas);
		this.backgroundDrawable.draw(canvas);
		uploadShapes(canvas);
		for(ShapeDrawable drawable: lineShapes) {
			drawable.draw(canvas);
		}
	}
	public Divider getDivider() {
		return divider;
	}
	public void setDivider(Divider divider) {
		this.divider = divider;
		this.dividerShape = null;
	}
	public void addLine(Position start, Position end, Axis axis) {
		Double top;
		Double right;
		Double bottom;
		Double left;
		if(Axis.AXIS_X.equals(axis)) {
			top = start.getY()-2;
			right = end.getX();
			bottom = top + 4;
			left = start.getX();
		} else {
			top = start.getY();
			right = start.getX()+2;
			bottom = end.getY();
			left = right - 4;
		}
		ShapeDrawable lineShape = new ShapeDrawable(new RectShape());
		lineShape.setBounds(left.intValue(), top.intValue(), right.intValue(), bottom.intValue());
//		lineShape.getPaint().setColor(0xff74AC23);
		lineShape.getPaint().setColor(0xff000000);
		this.lineShapes.add(lineShape);
		
	}
}
